package com.kj.xuyuan.pop;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.kj.xuyuan.R;
import com.kj.xuyuan.base.BaseModel;
import com.kj.xuyuan.http.JsonCallback;
import com.kj.xuyuan.http.OkGoUtil;
import com.kj.xuyuan.http.UrlConstants;
import com.kj.xuyuan.model.SignBean;
import com.kj.xuyuan.util.DisplayUtil;
import com.kj.xuyuan.util.LogUtils;
import com.kj.xuyuan.util.ScreenUtils;
import com.kj.xuyuan.util.ToastUtil;
import com.lzy.okgo.model.Response;

import androidx.constraintlayout.widget.ConstraintLayout;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 签到弹窗
 */
public class SignPopWindow extends PopupWindow {

    @BindView(R.id.tv_sign)
    TextView tvSign;
    @BindView(R.id.iv_close)
    ImageView ivClose;
    @BindView(R.id.tv_day_one)
    TextView tvDayOne;
    @BindView(R.id.iv_gold_day_one)
    ImageView ivGoldDayOne;
    @BindView(R.id.ll_sign_day_one)
    LinearLayout llSignDayOne;
    @BindView(R.id.tv_day_two)
    TextView tvDayTwo;
    @BindView(R.id.iv_gold_day_two)
    ImageView ivGoldDayTwo;
    @BindView(R.id.ll_sign_day_two)
    LinearLayout llSignDayTwo;
    @BindView(R.id.tv_day_three)
    TextView tvDayThree;
    @BindView(R.id.iv_gold_day_three)
    ImageView ivGoldDayThree;
    @BindView(R.id.ll_sign_day_three)
    LinearLayout llSignDayThree;
    @BindView(R.id.tv_day_four)
    TextView tvDayFour;
    @BindView(R.id.iv_gold_day_four)
    ImageView ivGoldDayFour;
    @BindView(R.id.ll_sign_day_four)
    LinearLayout llSignDayFour;
    @BindView(R.id.tv_day_five)
    TextView tvDayFive;
    @BindView(R.id.iv_gold_day_five)
    ImageView ivGoldDayFive;
    @BindView(R.id.ll_sign_day_five)
    LinearLayout llSignDayFive;
    @BindView(R.id.tv_day_six)
    TextView tvDaySix;
    @BindView(R.id.iv_gold_day_six)
    ImageView ivGoldDaySix;
    @BindView(R.id.ll_sign_day_six)
    LinearLayout llSignDaySix;
    @BindView(R.id.tv_big_gift)
    TextView tvBigGift;
    @BindView(R.id.iv_big_gift)
    ImageView ivBigGift;
    @BindView(R.id.tv_day_end)
    TextView tvDayEnd;
    @BindView(R.id.cl_day_seven)
    ConstraintLayout clDaySeven;
    private Activity mContext;
    private View view;

    private SignBean data;

    public SignPopWindow(Activity context, SignBean data) {
        super(context);
        this.mContext = context;
        this.data = data;
        // 初始化页面
        init();
    }

    // 初始化布局
    private void init() {
        final LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        view = inflater.inflate(R.layout.layout_pop_sign, null);
        ButterKnife.bind(this, view);
        setFocusable(true);

        initView();
        //rvSign item  item_sign  adapter  看韩国社交的SignAdapter写

        // 导入布局
        this.setContentView(view);
        this.setClippingEnabled(false);
        setWidth(ScreenUtils.getScreenWidth(mContext)- DisplayUtil.dp2px(mContext,68));
        setHeight(WindowManager.LayoutParams.WRAP_CONTENT);
        setOutsideTouchable(true);
        final ColorDrawable dw = new ColorDrawable(0x0000000);
        setBackgroundDrawable(dw);
        setAlpha(0.7f);
    }

    private void initView() {
        if(data.getCount() > 0){
            llSignDayOne.setBackgroundResource(R.drawable.shape_gray_bg_5);
            tvDayOne.setTextColor(mContext.getResources().getColor(R.color.gray9));
            ivGoldDayOne.setImageResource(R.mipmap.icon_gold_gray);
        }

        if(data.getCount() > 1){
            llSignDayTwo.setBackgroundResource(R.drawable.shape_gray_bg_5);
            tvDayTwo.setTextColor(mContext.getResources().getColor(R.color.gray9));
            ivGoldDayTwo.setImageResource(R.mipmap.icon_gold_gray);
        }

        if(data.getCount() > 2){
            llSignDayThree.setBackgroundResource(R.drawable.shape_gray_bg_5);
            tvDayThree.setTextColor(mContext.getResources().getColor(R.color.gray9));
            ivGoldDayThree.setImageResource(R.mipmap.icon_gold_gray);
        }

        if(data.getCount() > 3){
            llSignDayFour.setBackgroundResource(R.drawable.shape_gray_bg_5);
            tvDayFour.setTextColor(mContext.getResources().getColor(R.color.gray9));
            ivGoldDayFour.setImageResource(R.mipmap.icon_gold_gray);
        }

        if(data.getCount() > 4){
            llSignDayFive.setBackgroundResource(R.drawable.shape_gray_bg_5);
            tvDayFive.setTextColor(mContext.getResources().getColor(R.color.gray9));
            ivGoldDayFive.setImageResource(R.mipmap.icon_gold_gray);
        }

        if(data.getCount() > 5){
            llSignDaySix.setBackgroundResource(R.drawable.shape_gray_bg_5);
            tvDaySix.setTextColor(mContext.getResources().getColor(R.color.gray9));
            ivGoldDaySix.setImageResource(R.mipmap.icon_gold_gray);
        }

        if(data.getCount() > 6){
            clDaySeven.setBackgroundResource(R.drawable.shape_gray_bg_5);
            tvDayEnd.setTextColor(mContext.getResources().getColor(R.color.gray9));
        }
    }

    @Override
    public void dismiss() {
        setAlpha(1);
        super.dismiss();
    }

    private void setAlpha(float alpha) {
        WindowManager.LayoutParams params = mContext.getWindow().getAttributes();
        params.alpha = alpha;
        mContext.getWindow().setAttributes(params);
    }

    @OnClick({R.id.tv_sign, R.id.iv_close})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_sign:
                if(data.getIs_sign() == 1){
                    ToastUtil.showToast(mContext,"今日已签到");
                    return;
                }
                sign();
                break;
            case R.id.iv_close:
                dismiss();
                break;
        }
    }

    private void sign() {
        OkGoUtil.postRequest(mContext, UrlConstants.SIGN, null, new JsonCallback<BaseModel<?>>() {
            @Override
            public void onSuccess(Response<BaseModel<?>> response) {
                if (response != null && response.body() != null) {
                    if(response.body().getCode() == 1){
                        dismiss();
                    }
                    ToastUtil.showToast(mContext,response.body().getMsg());
                }
            }

            @Override
            public void onError(Response<BaseModel<?>> response) {
                super.onError(response);
                LogUtils.e("TAG",response.getException().getMessage());
            }
        });
    }
}
